package newXgame.utils.list.sub
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import newXgame.utils.list.event.*;
	import newXgame.utils.list.struct.SuperStruct;

	public class SubMenuIteam extends Sprite
	{		
		private var m_sprIcon:Sprite;		//	图标
		private var m_txtTitle:ExTextField;	//	标题
		private var m_sprBg:Sprite;			//	背景图
				
		private var m_menuData:SuperStruct;	//	菜单结构
		private var m_selectData:SuperStruct;	//	当前点击对象的数据内容
		
		private var m_colorOver:uint;	//	鼠标移上时的颜色
		private var m_colorOut:uint;	//	鼠标移开时的颜色
		
		private var subArray:Array;		//	子对象列表
		private var showLength:Number;	//	显示列表的高度		
		
		public function SubMenuIteam(data:SuperStruct)
		{
			m_menuData = data;
			m_selectData = data;
			
			m_colorOver = ListGlobal.LightColor;
			m_colorOut = ListGlobal.BaseColor;
			
			showLength = 0;
			
			subArray = new Array();			

			InitObject();
			InitListener();
			InitSubObject();					
		}
		
		//	返回自己的数据
		public function get data():SuperStruct
		{
			return this.m_menuData;
		}
		
		//	返回当前点击对象的数据内容
		public function get clickData():SuperStruct
		{
			return this.m_selectData;
		}
		
		//	设置点击标识
		public function set isClick(flag:Boolean):void
		{
			this.data.isShow = flag;
			
			DrawRollOut();
			
			if(flag)
			{
				ShowSubObject();
			}
			else
			{
				HiddenSubObject();
				this.dispatchEvent(new Event(IMGEvent.HIDDEN_MENU));
			}			
		}

		//	返回当前对象的展开状态
		public function get isClick():Boolean
		{
			return this.data.isShow;
		}
		
		//	取得当前动作的高度
		public function get length():Number
		{
			showLength = 1;
			if(this.data.isShow)
			{
				showLength += this.subArray.length;
				for(var i:int = 0; i < this.subArray.length; i++)
				{
					var ms:SubMenuIteam = this.subArray[i] as SubMenuIteam;
					if(ms.data.isShow)
					{
						showLength += Number(ms.length - 1);
					}
				}
			}
			return showLength;
		}
		
		
		//	取得对象的高度
		public function get objectHeight():Number
		{
			return this.m_txtTitle.height;
		}
		
		//	取得对象的整体高度
		public override function get height():Number
		{
			return this.objectHeight * this.length;
		}
			
		//	隐藏自己
		public function Hidden():void
		{
			for(var m:int = 0; m < this.subArray.length; m++)
			{
				var subIteam:SubMenuIteam = this.subArray[m];
				subIteam.removeEventListener(IMGEvent.SHOW_MENU, onAddLength);
				subIteam.removeEventListener(IMGEvent.HIDDEN_MENU, onDelLength);
				subIteam.Hidden();
			}
			
			this.m_sprBg.removeEventListener(MouseEvent.ROLL_OVER, onRollOverEvent);
			this.m_sprBg.removeEventListener(MouseEvent.ROLL_OUT, onRollOutEvent);
			this.m_sprBg.removeEventListener(MouseEvent.CLICK, onClickEvent);
			
			this.parent.removeChild(this);
		}
				
		private function InitObject():void
		{
			m_sprIcon = new Sprite();
			m_txtTitle = new ExTextField();
			m_sprBg = new Sprite();
			
			this.addChild(m_sprBg);
			this.addChild(m_sprIcon);
			this.addChild(m_txtTitle);
			
			DrawRollOut();
		}
		
		private function InitListener():void
		{
			this.m_sprIcon.mouseChildren = this.m_sprIcon.mouseEnabled = false;
			this.m_txtTitle.mouseChildren = this.m_txtTitle.mouseEnabled = false;
			
			this.m_sprBg.addEventListener(MouseEvent.ROLL_OVER, onRollOverEvent);
			this.m_sprBg.addEventListener(MouseEvent.ROLL_OUT, onRollOutEvent);
			this.m_sprBg.addEventListener(MouseEvent.CLICK, onClickEvent);
		}
		
		private function onRollOverEvent(e:MouseEvent):void
		{
			this.DrawRollOver();
		}
		
		private function onRollOutEvent(e:MouseEvent):void
		{
			this.DrawRollOut();
		}
		
		//	对象被点击后触发事件
		private function onClickEvent(e:MouseEvent):void
		{
			this.m_selectData = this.data;
			
			this.isClick = !this.isClick;			
			this.dispatchEvent(new Event(IMGEvent.CLICK_THIS));
			
			DrawRollOver();
		}
		
		//	绘制鼠标移上事件
		private function DrawRollOut():void
		{			
			this.RushIcon(m_colorOut);
			this.RushText(m_colorOut);
			this.RushBg(m_colorOut);			
		}
		
		//	绘制鼠标移开事件
		private function DrawRollOver():void
		{
			this.RushIcon(m_colorOver);
			this.RushText(m_colorOver);
			this.RushBg(m_colorOver);			
		}
		
		//	刷新图标
		private function RushIcon(col:uint):void
		{
			this.m_sprIcon.graphics.clear();
			this.m_sprIcon.graphics.lineStyle(1, col, 1);
			this.m_sprIcon.graphics.beginFill(col, 1);
			this.m_sprIcon.graphics.drawRect(6, 10, 2, 2);
			this.m_sprIcon.graphics.endFill();
			
			//	绘制 符号			
			if(this.m_menuData is MenuStruct)			
			{
				var ms:MenuStruct = this.m_menuData as MenuStruct;				
				
				if(ms.subArray.length > 0 || ms.dataArray.length > 0)
				{
					this.m_sprIcon.graphics.clear();
					this.m_sprIcon.graphics.lineStyle(1, col, 1);
					this.m_sprIcon.graphics.drawRect(2, 6, 8, 8);
			
					this.m_sprIcon.graphics.moveTo(2, 10);
					this.m_sprIcon.graphics.lineTo(10, 10);
					if(!this.isClick)
					{
						this.m_sprIcon.graphics.moveTo(6, 6);
						this.m_sprIcon.graphics.lineTo(6, 14);
					}
				}
			}
		}
		
		//	刷新文本内容
		private function RushText(col:uint):void
		{
			//	更新文本
			this.m_txtTitle.text = this.m_menuData.objName;						
			this.m_txtTitle.x = 12;
			this.m_txtTitle.color = col;
		}
		
		//	刷新背景
		private function RushBg(col:uint):void
		{
			//	更新热响应区
			this.m_sprBg.graphics.clear();
			this.m_sprBg.graphics.beginFill(col, 0);
			this.m_sprBg.graphics.drawRect(0, 0, this.m_txtTitle.width + 15, 20);
			this.m_sprBg.graphics.endFill();
		}
		
		//---------------------------------------------------------
		
		//	初始化子类对象
		private function InitSubObject():void
		{
			var dep:Number = 1;	
		
			//	如意对象有子类
			if(Boolean(this.m_menuData is MenuStruct))
			{
				var ms:MenuStruct = this.m_menuData as MenuStruct;
				var subIteam:SubMenuIteam;
				
				trace("now List :: " + ms.subArray.length + " :: " + ms.dataArray.length);
				
				for(var i:int = 0; i < ms.subArray.length; i++)
				{				
					subIteam = new SubMenuIteam(ms.subArray[i]);
					this.addChild(subIteam);
					
					subArray.push(subIteam);
					
					subIteam.x = this.x + 15;
					subIteam.y = subIteam.objectHeight * subArray.length;					
				}

				for(var j:int = 0; j < ms.dataArray.length; j++)
				{
					subIteam = new SubMenuIteam(ms.dataArray[j]);
					this.addChild(subIteam);
					
					subArray.push(subIteam);
					
					subIteam.x = this.x + 15;
					subIteam.y = subIteam.objectHeight * subArray.length
				}
				
				for(var m:int = 0; m < this.subArray.length; m++)
				{
					subIteam = this.subArray[m];
					subIteam.addEventListener(IMGEvent.SHOW_MENU, onAddLength);
					subIteam.addEventListener(IMGEvent.HIDDEN_MENU, onDelLength);
					subIteam.addEventListener(IMGEvent.CLICK_THIS, onClickSub);
				}
			}
			
			this.isClick = this.data.isShow;
		}
		
		//	展开
		private function onAddLength(e:Event):void
		{
			RushMenuPlace(e.target as SubMenuIteam);
			
			this.dispatchEvent(e);//new Event(IMGEvent.SHOW_MENU));
		}
		
		//	收回
		private function onDelLength(e:Event):void
		{
			RushMenuPlace(e.target as SubMenuIteam);
					
			this.dispatchEvent(e);//new Event(IMGEvent.HIDDEN_MENU));
		}
		
		//	对象被点击事件
		private function onClickSub(e:Event):void
		{
			this.m_selectData = (e.target as SubMenuIteam).clickData;
			
			this.dispatchEvent(e);
		}
		
		//	显示菜单
		private function ShowSubObject():void
		{
			if(this.subArray.length > 0)
			{
				showLength = this.subArray.length;
				
				for(var i:int = 0; i < this.subArray.length; i++)
				{
					this.subArray[i].visible = true;
				}
						
				RushMenuPlace(this);
			
				this.dispatchEvent(new Event(IMGEvent.SHOW_MENU));
			}
		}
		
		//	隐藏菜单
		private function HiddenSubObject():void
		{
			if(this.subArray.length > 0)
			{
				showLength = this.subArray.length;
				
				for(var i:int = 0; i < this.subArray.length; i++)
				{
					(this.subArray[i] as SubMenuIteam).visible = false;
					(this.subArray[i] as SubMenuIteam).isClick = false;
				}
				
				RushMenuPlace(this);
				
				this.dispatchEvent(new Event(IMGEvent.HIDDEN_MENU));
			}
		}
		
		/** 刷新菜单对象的位置 **/
		public function RushMenuPlace(mIteam:DisplayObject):void
		{
			if(mIteam.parent)
			{
				for(var j:int = 0; j < mIteam.parent.numChildren; j++)
				{
					if(j > mIteam.parent.getChildIndex(mIteam))
					{
						var fSpr:DisplayObject = mIteam.parent.getChildAt(j-1);
						var spr:DisplayObject = mIteam.parent.getChildAt(j);
						
						spr.y = fSpr.y + fSpr.height;
					}
				}
			}
		}
	}
}